'''
集合：不允许有重复元素
'''

set1 = {1, 2, 3, 5, 3, 2}
print(set1) # {1, 2, 3, 5}
print(len(set1)) # 4
# 创建集合的构造器语法(面向对象部分会进行详细讲解)
set2 = set(range(1, 10))
set3 = set((1, 2, 3, 3, 2, 1))
print(set2, set3) #{1, 2, 3, 4, 5, 6, 7, 8, 9} {1, 2, 3}
# 创建集合的推导式语法(推导式也可以用于推导集合)
set4 = {num for num in range(1, 100) if num % 3 == 0 or num % 5 == 0}
print(set4)

'''
集合添加删除元素
'''
set1.add(4)
set1.add(9)
print(set1) # {1, 2, 3, 4, 5, 9}
# 更新集合，追加到后面
set2.update([11, 12])
print(set2) # {1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12}
# 删除元素
set2.discard(5)
print(set2) # {1, 2, 3, 4, 6, 7, 8, 9, 11, 12}
if 4 in set2:
    set2.remove(4)
print(set1, set2) # {1, 2, 3, 4, 5, 9} {1, 2, 3, 6, 7, 8, 9, 11, 12}
print(set3.pop()) # 1
print(set3) # {2,3}

'''
交集 差集 并集
'''
# 交集
print(set1 & set2) # {1, 2, 3, 9}
# print(set1.intersection(set2)) # {1, 2, 3, 9}
# 并集
print(set1 | set2) # {1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12}
# print(set1.union(set2)) # {1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12}
# 差集 set1中set2中没有的
print(set1 - set2) # {4, 5}
# print(set1.difference(set2)) # {4, 5}
# 对称差 只有一个集合有的元素
print(set1 ^ set2) # {4, 5, 6, 7, 8, 11, 12}
# print(set1.symmetric_difference(set2)) # {4, 5, 6, 7, 8, 11, 12}
# 判断子集 超集
print(set2 <= set1) # False
# print(set2.issubset(set1)) # False
print(set3 <= set1) # True
# print(set3.issubset(set1)) # True
# 超集
print(set1 >= set2) # False
# print(set1.issuperset(set2))
print(set1 >= set3) # True
# print(set1.issuperset(set3))